module Sustainalytics.Accounts.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;

    export class UsersService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;
        private $location: ng.ILocationService;

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            $location: ng.ILocationService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects,
            $upload: any) {

            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjectsAdmin = staticObjectsAdmin;
            this.$location = $location;
        }

        private usersUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/accountsserv/api/v1/users/";
        }

        public usersGetCount(status: string, accountIds: string[], name: string): ng.IPromise<number> {

            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.usersUrl(), { params: $.extend({ getCount: true }, { accountIds: accountIds, status: status, name: name }) });
            promise
                .success(data => { def.resolve(parseInt(<string>data)); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public usersGetLastCrmSync(): ng.IPromise<string> {
            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.usersUrl(), { params: { returnLastCrmSyncDate: true } });
            promise
                .success(data => {
                    var d = data;
                    def.resolve(data);
                })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }


        public usersGetList(gridOpt: CommonAdmin.HttpGetOptionsDto, accountIds: string[], status: string, name: string): ng.IPromise<UsersDto[]> {

            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.usersUrl(), { params: $.extend({ accountIds: accountIds, status: status, name: name }, gridOpt) });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public usersGetAccountsList(gridOpt: CommonAdmin.HttpGetOptionsDto, status: string, name: string): ng.IPromise<AccountsDto[]> {

            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.usersUrl(), { params: $.extend({ status: status, name: name, returnAccounts: true }, gridOpt) });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public userGet(id: string): ng.IPromise<UsersDto> {
            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.usersUrl() + id, {});
            promise
                .success(data=> { def.resolve(data); })
                .error(err=> { ctx.showError(err); def.reject(); });

            return def.promise;
        }

        public usersResetPasswords(items: string[]): ng.IPromise<boolean> {

            var ctx = this;
            var def = this.$q.defer();

            var newop = new AccountsPatchOpt();
            newop.resourceIds = items;
            newop.verb = "RESETPASSWORD";
            var ops: AccountsPatchOpt[] = [newop];

            var promise = ctx.$http({ method: "PATCH", url: ctx.usersUrl(), data: ops });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjectsAdmin.getMessageByInternalCode(err['code']));
        }

    }


    var mod = angular.module(accountAdminModuleName);

    mod.service('UsersService',
        ['$q', '$http', '$location', 'notificationFactory', 'staticObjectsAdmin', '$upload', UsersService]);
}

        
